from rest_framework.generics import ListCreateAPIView

from users.models import User
from meiduo_admin.serializers.users import UserSerializer
from meiduo_admin.utils import PageNum


class UserView(ListCreateAPIView):
    """查询用户信息"""
    # 指定序列化器
    serializer_class = UserSerializer
    # 使用分页器
    pagination_class = PageNum

    # 重写获取查询集方法
    def get_queryset(self):
        if self.request.query_params.get('keyword') == '':
            return User.objects.all()
        else:
            return User.objects.filter(username__contains=self.request.query_params.get('keyword'))
